package com.abe.config;

import java.util.Arrays;

import android.annotation.SuppressLint;

import com.abe.mvp.model.bean.DeviceBean;
import com.abe.mvp.model.bean.SerialPortDataBean;
import com.abe.mvp.model.bean.SerialPortParseBean;
import com.abe.utils.CRCChecker;

/** 跑步机串口相关 */
@SuppressLint("UseValueOf")
public class StrengthtrainerSericalPortUtils {

	// 01 03 0C 00 0A 00 02 00 01 00 00 00 00 00 00 BB 08
	/** 协议相关 */
	/** 根据地址区分功能 */
	public static final byte address_all_read = 0x00; // 读全部
	public static final byte address_weight = 0x01; // 重量（kg）
	public static final byte address_height = 0x02;// 拉起高度（cm）
	public static final byte address_max_height = 0x03;// 最大高度（cm）
	public static final byte address_count = 0x04;// 锻炼次数
	public static final byte address_record_end = 0x05;// 记录完毕
	public static final byte address_control = 0x06;// 启停控制
	public static final byte address_prepara = 0x07;// 等待： 一组锻炼的开始的准备时间
	public static final byte address_push = 0x08;// 推举：开始位置至最大幅度时间
	public static final byte address_keep_max = 0x09;// 保持：最高位置保持时间
	public static final byte address_return = 0x0a;// 回撤：最大幅度至开始位置时间

	/** 信息全读返回数据长度 */
	private static final byte all_data_read_len = 25;
	/** 读操作or写操作 */
	public static final byte type_read = 0x03;
	public static final byte type_write = 0x06;
	public static final byte type_push  = 0x02;
	public static final byte push_flag=(byte) 0xaa;

	/**************************** 跑步返回数据解析 ******************************/

	public static SerialPortParseBean parse(SerialPortDataBean bean) {
		SerialPortParseBean parseData = new SerialPortParseBean();
		/** 返回原始数据 */
		byte[] data = bean.getBuffer();
		/** 读操作or写操作 */
		byte userTypeData = data[1];
		byte[] available;
		boolean verifyStatus;
		byte address;
		int userType;
		if (userTypeData == type_read) {
			/** 读操作数据解析 */
			userType = 0;
			address = address_all_read;
			if (data.length < all_data_read_len) {
				verifyStatus = false;
			} else {
				available = Arrays.copyOf(data, all_data_read_len);
				verifyStatus = CRCChecker.checkBuf(available);
				if (verifyStatus) {
					DeviceBean deviceBean = new DeviceBean();
					int weight = byteToInt(available[4]);
					int height = byteToInt(available[5]) * 256
							+ byteToInt(available[6]);
					int maxHeight = byteToInt(available[7]) * 256
							+ byteToInt(available[8]);
					int count = byteToInt(available[10]);
					boolean recordEnd = byteToInt(available[12]) == 1 ? true
							: false;
					boolean control = byteToInt(available[14]) == 1 ? true
							: false;
					// int preparaTime = byteToInt(available[16]);
					// int pushTime = byteToInt(available[18]);
					// int keepMaxTime = byteToInt(available[20]);
					// int returnTime = byteToInt(available[22]);
					int preparaTime = 200;
					int pushTime = 1000;
					int keepMaxTime = 200;
					int returnTime = 400;
					deviceBean.setWeight(weight);
					deviceBean.setHeight(height);
					deviceBean.setMaxHeight(maxHeight);
					deviceBean.setCount(count);
					deviceBean.setRecordEnd(recordEnd);
					deviceBean.setControl(control);
					deviceBean.setPreparaTime(preparaTime);
					deviceBean.setPushTime(pushTime);
					deviceBean.setKeepMaxTime(keepMaxTime);
					deviceBean.setReturnTime(returnTime);
					parseData.setReadData(deviceBean);
				}
			}
		} else {
			/** 写操作数据解析 */
			userType = 1;
			if (data.length < 6) {
				verifyStatus = false;
				address = 0x00;
			} else {
				available = Arrays.copyOf(data, 6);
				address = available[3];
				verifyStatus = CRCChecker.checkBuf(available);
			}
		}
		parseData.setVerifyStatus(verifyStatus);
		parseData.setAddress(address);
		parseData.setUseType(userType);
		return parseData;
	}
	public static SerialPortParseBean pushDataParse(SerialPortDataBean bean) {
		SerialPortParseBean parseData = new SerialPortParseBean();
		/** 返回原始数据 */
		boolean verifyStatus;
		byte[] data = bean.getBuffer();
		byte[] available=new byte[]{};
		for (int i = 0; i < data.length; i++) {
			byte temData=data[i];
			if (push_flag==temData&&i<39) {
				available=Arrays.copyOfRange(data, i, all_data_read_len);
			}
		}
		if (data.length < all_data_read_len) {
			verifyStatus = false;
		} else {
			verifyStatus = CRCChecker.checkBuf(available);
			if (verifyStatus) {
				DeviceBean deviceBean = new DeviceBean();
				int weight = byteToInt(available[5]);
				int height = byteToInt(available[6]) * 256
						+ byteToInt(available[7]);
				int maxHeight = byteToInt(available[8]) * 256
						+ byteToInt(available[9]);
				int count = byteToInt(available[11]);
				boolean recordEnd = byteToInt(available[13]) == 1 ? true
						: false;
				boolean control = byteToInt(available[15]) == 1 ? true
						: false;
				// int preparaTime = byteToInt(available[16]);
				// int pushTime = byteToInt(available[18]);
				// int keepMaxTime = byteToInt(available[20]);
				// int returnTime = byteToInt(available[22]);
				int preparaTime = 200;
				int pushTime = 1000;
				int keepMaxTime = 200;
				int returnTime = 400;
				deviceBean.setWeight(weight);
				deviceBean.setHeight(height);
				deviceBean.setMaxHeight(maxHeight);
				deviceBean.setCount(count);
				deviceBean.setRecordEnd(recordEnd);
				deviceBean.setControl(control);
				deviceBean.setPreparaTime(preparaTime);
				deviceBean.setPushTime(pushTime);
				deviceBean.setKeepMaxTime(keepMaxTime);
				deviceBean.setReturnTime(returnTime);
				parseData.setReadData(deviceBean);
			}
		}
		
		parseData.setVerifyStatus(verifyStatus);
		parseData.setUseType(2);
		return parseData;
	}
	public static int byteToInt(byte data) {
		return data & 0xFF;
	}

	/**************************** 命令集合 ******************************/

	/**
	 * @see 设置启动 1---0x01
	 * @see 设置关闭 0---0x00
	 * */
	public static byte[] SET_START = { (byte) 0x01, (byte) 0x06, (byte) 0x00,
			(byte) 0x06, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00 };

	public static byte[] SET_STOP = { (byte) 0x01, (byte) 0x06, (byte) 0x00,
			(byte) 0x06, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 };

	/**
	 * @see 读数据
	 * @see 重量（kg）
	 * @see 拉起高度（cm）
	 * @see 最大高度（cm）
	 * @see 锻炼次数
	 * @see 记录完毕
	 * @see 启停控制
	 * @see 等待： 一组锻炼的开始的准备时间
	 * @see 推举：开始位置至最大幅度时间
	 * @see 保持：最高位置保持时间
	 * @see 回撤：最大幅度至开始位置时间
	 */
	public static byte[] READ_DATA = { (byte) 0x01, (byte) 0x03, (byte) 0x00,
			(byte) 0x01, (byte) 0x00, (byte) 0x0a, (byte) 0x00, (byte) 0x00 };

}